<template>
  <div class="birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="onCancel"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import {updateUser} from '@/api/user'
import dayjs from 'dayjs'

export default {
  name: 'update-birthday',
  props: ['value'],
  data() {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  methods: {
    async onConfirm () {
      try {
        const birthday = dayjs(this.currentDate).format('YYYY-MM-DD')

        await updateUser({
          birthday
        })

        this.$emit('close')
        this.$emit('input', birthday)
        this.$toast('修改生日成功')
      } catch (err) {
        this.$toast('修改生日失败')
      }

    },
    onCancel() {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
